/**
 *	bbx 
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');

	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var systemService = require('System_Service');
	var accountService = require('Account_Service');
	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			mui.init({
				statusBarBackground: '#f7f7f7'
			});
			WindowTools.preloadTemplate();
			initReg();
			regClick();
		});
	}
	
	function regClick(){
		mui('.mui-content').on('tap', '#reg', function() {
			console.log("click reg");
			reg();
		});
		
		mui('.mui-content').on('tap', '#refresh', function() {
			console.log("refresh");
			document.getElementById('refresh').style.display = 'none';
			initReg();
		});
	}

	function initReg() {
		systemService.getCitys(function(data) {
			//			debugger;
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var option;
				option = document.createElement('option');
				option.setAttribute("value", item.id);
				option.innerHTML = item.name;
				fragment.appendChild(option);
			}
			var dataContainer = document.getElementById('citys');
			dataContainer.appendChild(fragment);
		},function(){
			document.getElementById('refresh').style.display = '';
		});
	}

	function reg() {
		var account = document.getElementById('account').value;
		var password1 = document.getElementById('password').value;
		var password2 = document.getElementById('password_confirm').value;
		var citySelect = document.getElementById('citys');
		var index = citySelect.selectedIndex;
		var city = citySelect.options[index].value;
		if(account == ''){
			mui.toast('用户名不能为空');
			return;
		}
		if(password1 == ''){
			mui.toast('密码不能为空');
			return;
		}
		if(password2 != password1){
			mui.toast('两次密码不一样');
			return;
		}
		if(city == ''){
			mui.toast('城市不能为空');
			return;
		}
		//TODO 需要判断
		var data = {
			"UserName": account,
			"Password": password1,
			"ConfirmPassword": password2,
			"CityID": city
		}
		
		accountService.register(data, function() {
			mui.toast('注册成功', {
				duration: 'short',
				type: 'div'
			});
			WindowTools.closeCurrentPage();
		})
	}
});